<?php

/**
 * 修复批次记录
 * @author HelloWorld
 * 2023-8-8
 * UTF-8
 */
class RepairrecordController extends Controller_Base
{
    //修复记录
    public function IndexAction()
    {
        $request = $this->getRequest();
        $page = intval($request->getQuery('page', 1));
        $limit = intval($request->getQuery('limit', 10));
        $batchNo = $request->getQuery('batchNo', '');
        $repairType = $request->getQuery('repairType', 0);
        $idCard = $request->getQuery('idCard', 0);
        $repairName = $request->getQuery('repairName', 0);
        $mobileStatus = $request->getQuery('mobileStatus', 0);
        $where = [];
        if($batchNo){
            array_push($where,"batchNo = '{$batchNo}'");
        }
        if($repairType){
            array_push($where,"repairType = '{$repairType}'");
        }
        if($idCard){
            array_push($where,"idCard = '{$idCard}'");
        }
        if($mobileStatus){
            array_push($where,"mobileStatus = '{$mobileStatus}'");
        }
        if($repairName){
            array_push($where,"repairName = '{$repairName}'");
        }
        $add_begin_end = trim($this->getRequest()->getQuery('add_begin_end', ''));
        if ($add_begin_end){
            $date_tmp =  explode(" 到 ",$add_begin_end);
            $begin_date = $date_tmp[0];
            $end_date = $date_tmp[1];
            if (!empty($begin_date) || !empty($end_date)) {
                $begin_date = $begin_date ? $begin_date . " 00:00:00" : "";
                $end_date = $end_date ? $end_date . " 23:59:59" : "";
            }
            if ($end_date) {
                $end_date = strtotime($end_date);
                array_push($where, "repairTime <= {$end_date}");

            }
            if ($begin_date) {
                $begin_date = strtotime($begin_date);
                array_push($where, "repairTime >= {$begin_date}");
            }
        }
        $page = $page ? $page : 1;
        $start = ($page - 1) * $limit;
        $query = [
            'batchNo' => $batchNo,
            'repairType' => $repairType,
            'idCard' => $idCard,
            'repairName' => $repairName,
            'add_begin_end' => $add_begin_end,
            'mobileStatus' => $mobileStatus,
            'limit' => $limit,
        ];
        $seacher = array(
            'where' => $where,
            'limit' => $limit,
            'start' => $start,
            'order' => 'id DESC',
            'group'=>'sha256idCard'
        );
        $batch = Module_Repair_RepairResult::getInstance()->getAllBySearch(array("*,count(*) as num"), $seacher, true);
        $total = Module_Repair_RepairResult::getInstance()->getGroupTotalBySearch(array("count(*) as num"), array('where' => $where,'group'=>'sha256idCard'), true);
        $total = $total[0]['num'];
        $totalPage = ceil($total / $limit);
        //赋值调解员
        $this ->getMediator();
        SmartyAdapter::assign('arrList', $batch);
        SmartyAdapter::assign('totalPage', $totalPage);
        SmartyAdapter::assign('totalnum', $total);
        SmartyAdapter::assign('pagesize', $limit);
        SmartyAdapter::assign('curPageNum', $page);
        SmartyAdapter::assign('query', $query);
        SmartyAdapter::assign('mobileStatus', Module_Mediate_CaseLitigant::$mobileStatus);
        SmartyAdapter::assign('repairStatus', Module_Repair_RepairBatch::$repairStatusArr);
        SmartyAdapter::assign('page_query', http_build_query($query));

    }

    public function exportinfoAction(){
        $request = $this->getRequest();
        $batchNo = $request->getQuery('batchNo', '');
        $repairType = $request->getQuery('repairType', 0);
        $idCard = $request->getQuery('idCard', 0);
        $repairName = $request->getQuery('repairName', 0);
        $mobileStatus = $request->getQuery('mobileStatus', 0);
        $where = [];
        if($batchNo){
            array_push($where,"batchNo = '{$batchNo}'");
        }
        if($repairType){
            array_push($where,"repairType = '{$repairType}'");
        }
        if($idCard){
            array_push($where,"idCard = '{$idCard}'");
        }
        if($mobileStatus){
            array_push($where,"mobileStatus = '{$mobileStatus}'");
        }
        if($repairName){
            array_push($where,"repairName = '{$repairName}'");
        }
        $add_begin_end = trim($this->getRequest()->getQuery('add_begin_end', ''));
        if ($add_begin_end){
            $date_tmp =  explode(" 到 ",$add_begin_end);
            $begin_date = $date_tmp[0];
            $end_date = $date_tmp[1];
            if (!empty($begin_date) || !empty($end_date)) {
                $begin_date = $begin_date ? $begin_date . " 00:00:00" : "";
                $end_date = $end_date ? $end_date . " 23:59:59" : "";
            }
            if ($end_date) {
                $end_date = strtotime($end_date);
                array_push($where, "repairTime <= {$end_date}");

            }
            if ($begin_date) {
                $begin_date = strtotime($begin_date);
                array_push($where, "repairTime >= {$begin_date}");
            }
        }
        $seacher = array(
            'where' => $where,
            'order' => 'sha256idCard DESC',
        );
        $list = Module_Repair_RepairResult::getInstance()->getAllBySearch(array("*"), $seacher, true);
        $down = [];
        $mobileStatus = Module_Mediate_CaseLitigant::$mobileStatus;
        if (!empty($list)) {
            foreach ($list as $k => $_info) {
                $down[$k]['repairName'] = $_info['repairName'];
                $down[$k]['sha256idCard'] = $_info['sha256idCard'];
                $down[$k]['repairType'] = $_info['repairType'] == 1 ? '移动' : '联通';
                $down[$k]['batchNo'] = $_info['batchNo'];
                $down[$k]['repairTime'] = date("Y-m-d H:i:s", $_info['repairTime']);
                $down[$k]['repairmobile'] = $_info['repairmobile'];
                $down[$k]['mediateStatus'] = $mobileStatus[$_info['mobileStatus']];
            }
        }
        $header = ['修复名字', 'Sha256身份证号', '运营商','运营商批次号','修复时间','修复结果','拨打号码状态'];
        Lib_XlsxExport::getInstance()->downExcel($down,$header,'修复记录' );
        exit;
    }

}
